package com.caimiao.miao.page.ticket;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.caimiao.miao.R;
import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.interfaces.DialogActivity;
import com.caimiao.miao.library.utils.PixelUtil;
import com.caimiao.miao.service.MiaoService;
import com.caimiao.miao.service.MiaoServiceConnection;
import com.caimiao.miao.widget.BasePromptDialog;
import com.caimiao.miao.widget.TicketStateDialog;

public class TicketActivity extends DialogActivity<ITicketContact.Presenter> implements ITicketContact.View {
    public static final String EXTRA_DATA = "DATA";
    private ImageView mIvHead;
    private TextView mTvName, mTvCount;
    private TicketStateDialog mTicketDialog;
    private MiaoServiceConnection conn;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_ticket;
    }

    @Override
    protected void initUI() {
        mIvHead = findViewById(R.id.ticketIvHead);
        mTvName = findViewById(R.id.ticketTvName);
        mTvCount = findViewById(R.id.ticketTvCount);
    }

    @Override
    protected void initLogic() {
        new TicketPresenter(this);
        Intent intent = new Intent(this, MiaoService.class);
        conn = new MiaoServiceConnection(mPresenter);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (mPresenter != null) mPresenter.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void init(TicketOut ticket) {
        if (ticket != null) {
            Glide.with(this).load(ticket.getHeadimgurl()).apply(RequestOptions.circleCropTransform()).into(mIvHead);
            mTvName.setText(ticket.getNickname());
        }
    }

    @Override
    public void setTicketNum(String num) {
        mTvCount.setText(num);
    }

    @Override
    public void setTicketState(int state) {
        switch (state) {
            case 0://出票成功
                dismissDialog();
                break;
            case 1://出票失败，错误：无票，请补票
                showErrorDialog(state, R.string.dialog_ticket_error_1);
                break;
            case 2://出票失败，错误：出口有纸，请取走
                showErrorDialog(state, R.string.dialog_ticket_error_2);
                break;
            case 3://出票失败，错误：切刀出现故障，请检查设备
                showErrorDialog(state, R.string.dialog_ticket_error_3);
                break;
            case 4://出票失败，错误：出现卡票，请检查设备
                showErrorDialog(state, R.string.dialog_ticket_error_4);
                break;
            case 5://
                break;
        }
    }

    @Override
    protected void onDestroy() {
//        mPresenter.restartTakeTicket();
        dismissDialog();
        unbindService(conn);
        super.onDestroy();
    }

    private void showErrorDialog(final int state, int text) {
        if (mTicketDialog == null) {
            mTicketDialog = new TicketStateDialog(this);
            mTicketDialog.setOnHideLister(new BasePromptDialog.OnDialogHideListener() {
                @Override
                public void hide() {
                    hideBottomNavigation();
                }
            });
        }
        mTicketDialog.setContent(text);
        mTicketDialog.setOnBtnClickListener(new BasePromptDialog.OnSimpleBtnClickListener() {
            @Override
            public void onLeft(BasePromptDialog dialog, View view) {
                mPresenter.setCloseTicket(getApplication(), state);
            }

            @Override
            public void onRight(BasePromptDialog dialog, View view) {
                mPresenter.setContinue(state);
            }
        });
        mTicketDialog.show();
    }

    private void dismissDialog() {
        if (mTicketDialog != null && mTicketDialog.isShowing())
            mTicketDialog.dismiss();
    }
}
